package cn.samir.sudoku.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView.LayoutParams;
import android.widget.ImageView;
import android.widget.TextView;
import cn.samir.sudoku.R;
import cn.samir.sudoku.core.ScaledSudokuView;
import cn.samir.sudoku.core.Sudokus;
import cn.samir.sudoku.keys.Conf;

public class GameGridViewAdapter extends AdapterBase<Sudokus> {

	Context context;

	public GameGridViewAdapter(Context context) {
		this.context = context;
	}

	@Override
	protected View getExView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder;
		int width = parent.getWidth();

		if (convertView == null) {
			convertView = View.inflate(context, R.layout.item_gridview_scale,
					null);
			// int width = parent.getWidth();
			// LayoutParams params = new LayoutParams(width / 3-3, width / 3);
			// convertView.setLayoutParams(params);
			viewHolder = new ViewHolder();
			viewHolder.tv1 = (TextView) convertView.findViewById(R.id.tv1);
			viewHolder.ssv1 = (ScaledSudokuView) convertView
					.findViewById(R.id.ssv1);
			viewHolder.iv1 = (ImageView) convertView.findViewById(R.id.iv1);
			viewHolder.tv2 = (TextView) convertView.findViewById(R.id.tv2);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		LayoutParams params = new LayoutParams(width / 3 - 1, width / 3);
		convertView.setLayoutParams(params);
		Sudokus sudokus = (Sudokus) getItem(position);
		viewHolder.tv1.setTextSize(width / 12f);
		viewHolder.tv1.setText((position + 1) + "");
		if(Conf.isShowId){
			String showContent = "0"+sudokus.sudokuid+"0"+sudokus.content.substring(9);
			viewHolder.ssv1.setContent(showContent);
		}else{
			viewHolder.ssv1.setContent(sudokus.content);
		}
		if (sudokus.passed != 0) {
			viewHolder.iv1.setVisibility(ImageView.VISIBLE);
			viewHolder.iv1.setImageResource(R.drawable.passed);
		} else {
			viewHolder.iv1.setVisibility(ImageView.GONE);
		}
		if (sudokus.score > 0) {
			viewHolder.tv2.setVisibility(TextView.VISIBLE);
			viewHolder.tv2.setText(sudokus.score + "");
		} else {
			viewHolder.tv2.setVisibility(TextView.GONE);
		}
		return convertView;
	}

	@Override
	protected void onReachBottom() {

	}

	class ViewHolder {
		TextView tv1;// 编号
		ScaledSudokuView ssv1;// 背景
		ImageView iv1;// 显示是否通过
		TextView tv2;// 显示分数
	}

}
